package card;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class Main {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>(7);
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        System.out.println(list);
        System.out.println(list.size());

//                           循环次数
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i) +" ");
        }
        System.out.println();
//
        for (int x:list) {
            System.out.print(x+" ");
        }



    }
//    将该数组中出现的每个零都复写一遍，并将其余的元素向右平移。不要在超过该数组长度的位置写入元素。请对输入的数组 就地 进行上述修改，不要从函数返回任何东西。
    public static int[] duplicateZeros(int[] arr) {
        for (int i = 0; i < arr.length-1; i++) {
            if (arr[i] == 0){
                for (int j = arr.length-1; j > i; j--) {
                    arr[j] = arr[j-1];
                }
//                还要跳过这个0
                i++;
            }
        }
        return arr;
    }

    public static void main5(String[] args) {
        int[] nums = {22,1,0,33,2,0,9};
        int[] ints = duplicateZeros(nums);
        for (int number : ints) {
            System.out.print(number + " ");
        }
    }

    public static int[] moveZeroes(int[] nums) {
//        重新定义一个数组
//        int j = 0;
//        int[] temp = new int[j];
//        for (int i = 0; i < nums.length-1; i++) {
//            if (nums[i] != 0){
//                temp[j] = nums[i];
//            }
//            j++;
//        }
//        while (temp.length != nums.length){
//                temp[j] = 0;
//                j++;
//        }
//        return temp;
        // 记录非零元素应该放置的位置
        int index = 0;
        // 遍历原数组，将非零元素往前挪动
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != 0) {
                nums[index] = nums[i];
                index++;
            }
        }
        // 将剩余位置补0
        while (index < nums.length) {
            nums[index] = 0;
            index++;
        }
        return nums;
    }
    

    public static void main3(String[] args) {
        int[] nums = {22,1,0,33,2,0,9};
        int[] ints = moveZeroes(nums);
        for (int number : ints) {
            System.out.print(number + " ");
        }
    }


    public static ArrayList<Integer> GetLeastNumbers_Solution (int[] input, int k) {
        if (k > input.length){
            return null;
        }
        // 1.先冒泡排序，从小到大
        for (int i = 0; i < input.length-1; i++) {
            boolean flag = false;
            for (int j = 0; j < input.length-1; j++) {
                if (input[j] > input[j+1]){
                    swap(input, j,j+1);
                    flag = true;
                }
            }
            if (flag == false){
                break;
            }
        }
        // 2.循环输出k个数，放入数组中
        ArrayList<Integer> arrayList = new ArrayList<>();
        for (int i = 0; i < k; i++) {
            arrayList.add(input[i]);
        }
        return arrayList;
    }

    private static void swap(int[] input, int j, int i) {
        int tmp = input[j];
        input[j] = input[i];
        input[i] = tmp;
    }

    public static void main2(String[] args) {
        int[] inputs = {1,5,6,66,2,50};
        ArrayList<Integer> arrayList = GetLeastNumbers_Solution(inputs,3);
        System.out.println(arrayList);
    }
    public static void main1(String[] args) {
        cardGame cardgame = new cardGame();
        List<Card> cards = cardgame.buyCard();
        System.out.println("买牌：");
        System.out.println(cards);
        System.out.println("洗牌");
        cardgame.shuffle(cards);
        System.out.println(cards);
    }
}
